/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package game.jetclan.main;

import com.jme3.app.Application;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.audio.AudioNode;
import com.jme3.audio.AudioRenderer;
import com.jme3.input.InputManager;
import com.jme3.math.Vector3f;
import com.jme3.niftygui.NiftyJmeDisplay;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;

/**
 *
 * @author cla_marwas
 */
public class MenuAppState extends JetclanAppState implements ScreenController {

    /**
     * Pausing and Unpausing
     *
     * You define what an AppState does when Paused or Unpaused, in the
     * setEnabled() and update() methods. Call myState.setEnabled(false) on all
     * states that you want to pause. Call myState.setEnabled(true) on all
     * states that you want to unpause.
     *
     */
    private Main mainApp;
    private Node rootNode;
    private AssetManager assetManager;
    private AppStateManager stateManager;
    private InputManager inputManager;
    private ViewPort viewPort;
    private AudioRenderer audioRenderer;
//    private Nifty nifty;
//    NiftyJmeDisplay niftyDisplay;
    private Node menuTopNode = new Node("Menu Top Node");  // some custom class fields...    
    private final static String NIFTY_XML_NAME = "Interface/ashestoashes_menu.xml";
    private final static String NIFTY_XML_NAME_INIT_SCREEN = "menuScreen";
    private final static String NIFTY_XML_NAME_SETUP_GAME_SCREEN = "setupGameScreen";
    private final static String NIFTY_XML_NAME_SETTINGS_SCREEN = "settingsScreen";
    private AudioNode audio_gun;
    private AudioNode audio_nature;

    public MenuAppState() {
        super(StateNameEnum.MENU);
    }

    public Node getMenuTopNode() {
        return menuTopNode;
    }

    @Override
    public void update(float tpf) {
        super.update(tpf);

        //rootNode.updateLogicalState(tpf);
        //rootNode.updateGeometricState();
    }

    @Override
    public void initialize(AppStateManager stateManager, Application appParam) {
        super.initialize(stateManager, appParam);
        mainApp = (Main) appParam;
        rootNode = mainApp.getRootNode();
        assetManager = mainApp.getAssetManager();
        stateManager = mainApp.getStateManager();
        inputManager = mainApp.getInputManager();
        viewPort = mainApp.getViewPort();
        audioRenderer = mainApp.getAudioRenderer();

        //Dummy so far
        initInputs();
        //initNiftyGui();
        initSounds();


    }

    @Override
    public void cleanup() {
        super.cleanup();
        // unregister all my listeners, detach all my nodes, etc...
        //mainApp.getRootNode().detachChild(menuTopNode); // modify scene graph...
//        viewPort.removeProcessor(niftyDisplay);
        //this.app.doSomethingElse();                 // call custom methods...
    }

    @Override
    public void setEnabled(boolean enabled) {
        // Pause and unpause
        super.setEnabled(enabled);

    }
//    // Note that update is only called while the state is both attached and enabled.
//    @Override
//    public void update(float tpf) {
//      // do the following while game is RUNNING
//      this.app.getRootNode().getChild("blah").scale(tpf); // modify scene graph...
//      //x.setUserData(...);                                 // call some methods...
//    }

    /**
     * Pick a Target Using the Mouse Pointer. <ol><li>Map "pick target" action
     * to a MouseButtonTrigger. <li>flyCam.setEnabled(false);
     * <li>inputManager.setCursorVisible(true); <li>Implement action in
     * AnalogListener (TODO).</ol>
     */
    private void initInputs() {
        //inputManager.addMapping("pick_unit", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
//        inputManager.addMapping("unpick_unit", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
//
//        inputManager.addListener(analogListener, "unpick_unit");
        //inputManager.addListener(analogListener, "pick_unit");
    }

    public void bind(Nifty nifty, Screen screen) {
        System.out.println("bind( " + screen.getScreenId() + ")");
    }

    public void onStartScreen() {
        System.out.println("onStartScreen");
    }

    public void onEndScreen() {
        System.out.println("onEndScreen");
    }

    public void quitGameNifyEvent() {
        mainApp.quitGame();
    }

    public void startGameNiftyEvent() {
        mainApp.startGame();
    }

    public void goToStartNiftyEvent() {
        audio_gun.play();
        nifty.gotoScreen(NIFTY_XML_NAME_INIT_SCREEN);
    }

    public void goToSetupNiftyEvent() {
        audio_gun.play();
        nifty.gotoScreen(NIFTY_XML_NAME_SETUP_GAME_SCREEN);
    }

    public void goToSettingsNiftyEvent() {
        audio_gun.play();
        nifty.gotoScreen(NIFTY_XML_NAME_SETTINGS_SCREEN);
    }

//    private void initNiftyGui() {
//        niftyDisplay = new NiftyJmeDisplay(assetManager, inputManager, audioRenderer, viewPort);
//        nifty = niftyDisplay.getNifty();
//        nifty.fromXml(NIFTY_XML_NAME, NIFTY_XML_NAME_INIT_SCREEN, this);// attach the nifty display to the gui view port as a processor         
//        viewPort.addProcessor(niftyDisplay);
//    }

    private void initSounds() {
        audio_gun = new AudioNode(assetManager, "Sounds/shoot_gun.wav", false);
        audio_gun.setLooping(false);
        audio_gun.setVolume(2);
        rootNode.attachChild(audio_gun);
        
         /* nature sound - keeps playing in a loop. */
        audio_nature = new AudioNode(assetManager, "Sound/Environment/Nature.ogg", false);
        audio_nature.setLooping(true);  // activate continuous playing
        audio_nature.setPositional(false);
        audio_nature.setLocalTranslation(Vector3f.ZERO.clone());
        audio_nature.setVolume(3);
        rootNode.attachChild(audio_nature);
        audio_nature.play(); // play continuously!

    }
}
